package com.gambi.tapestry.client.chatter.view;

import com.gambi.tapestry.client.chatter.model.Contact;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class ContactListView
// The reason for this is we don't want the contained widget to stretch the
		// entire height of the view
		// A SimplePanel wraps its child panel in an HTML div tag, while a
		// Composite contains the child widget directly.
		// Any CSS or sizing applied to the SimplePanel gets applied to its div
		// tag, but not to the
		// contained widget as it would with a Composite.

		extends SimplePanel {
	private class ContactClickListener implements ClickListener {
		Contact contact;

		ContactClickListener(Contact contact) {
			this.contact = contact;
		}

		public void onClick(Widget sender) {
			view.getChatWindowView(contact);
		}
	}

	private FlexTable listTable = new FlexTable();
	private MessengerView view;

	public ContactListView(final MessengerView view) {
		setWidget(listTable);
		this.view = view;
		listTable.setCellPadding(0);
		listTable.setCellSpacing(0);

		// The first row is the signout button
		Button signOut = new Button("Sign Out");
		signOut.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				onSignOut();
			}
		});

		listTable.setWidget(0, 0, signOut);

		addContact(view.getContactList().getMe());
		listTable.getRowFormatter().addStyleName(0, "myContactItem");
		listTable.getColumnFormatter().setWidth(1, "100%");
	}

	public void onSignOut(){
		GWT.log("ContactListView.onSignOut() with SignOut button");
		view.getListener().onSignOut();
	}
	
	public void addContact(Contact contact) {
		GWT.log("ContactListView.addContact() " + contact);
		GWT.log("ContactListView.addContact() " + contact.getName());
		if (contact == null) {
			return;
		}

		int row = listTable.getRowCount();
		Hyperlink link = new Hyperlink(contact.getName(), contact.getName()
				+ System.currentTimeMillis());
		listTable.getRowFormatter().setVerticalAlign(row,
				HasVerticalAlignment.ALIGN_TOP);
		listTable.setWidget(row, 0, new Image("icon_user.gif"));
		listTable.getRowFormatter().setStyleName(row, "contactItem");
		if (row != 0) {
			listTable.setWidget(row, 1, link);
			// listTable.setWidget(row, 1, new Label( contact.getName() ));
		} else {
			listTable.setText(row, 1, contact.getName());
		}
		link.addClickListener(new ContactClickListener(contact));
	}

	public void removeContact(Contact contact) {
		for (int i = 1; i < listTable.getRowCount();) {
			Hyperlink link = (Hyperlink) listTable.getWidget(i, 1);
			if (link.getText().equals(contact.getName()))
				listTable.removeRow(i);
			else
				++i;
		}
	}
}
